package com.mobile.check.present;

import android.util.Log;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * 类名: TaskItemDetailPresent
 * 作者: 李学伟
 * 日期: 2020年10月09日 14:40
 * 描述:
 */
public class TaskItemDetailHelper  {
    public void setExcel(int rowNum, int cellNum, String val, XSSFWorkbook workbook) throws IOException {
        XSSFSheet sheet = workbook.getSheetAt(0);// 获取第一个表
        // 如果已有行、列直接获取
        XSSFRow row = sheet.getRow(rowNum);// 得到行
        XSSFCell cell = row.getCell(cellNum);// 得到列
        XSSFCellStyle cellStyle = workbook.createCellStyle();
        cell = row.createCell(cellNum);
        cell.setCellStyle(cellStyle);
        cell.setCellValue(val);// 写数据
    }

    public void parserExcel(String filePath,final DetailHelperListener helperListener){
        Observable.just(filePath).map(new Function<String, ArrayList<HashMap<String, String>>>() {
            @Override
            public ArrayList<HashMap<String, String>> apply(String filePath) throws Exception {
                return getExcel(filePath);
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<ArrayList<HashMap<String, String>>>() {
            @Override
            public void accept(ArrayList<HashMap<String, String>> dataList) throws Exception {
                dataList.remove(0);
                helperListener.parserSuccess(dataList);
            }
        });

    }

    public ArrayList<HashMap<String, String>> getExcel(String doc) {
        ArrayList<HashMap<String, String>> res = null;
        try {
            // 设置要读取的文件路径
            InputStream inputStream = new FileInputStream(doc);
            XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
            XSSFSheet sheet = workbook.getSheetAt(0);
            int rowsCount = sheet.getPhysicalNumberOfRows();
            res = new ArrayList<HashMap<String, String>>(rowsCount);
            FormulaEvaluator formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();
            for (int r = 0; r < rowsCount; r++) {
                Row row = sheet.getRow(r);

                Cell v0 = row.getCell(0);
                Cell v1 = row.getCell(1);
                Cell v2 = row.getCell(2);
                Cell v3 = row.getCell(3);
                Cell v4 = row.getCell(4);
                Cell v5 = row.getCell(5);
                Cell v6 = row.getCell(6);
                Cell v7 = row.getCell(7);
                Cell v8 = row.getCell(8);
                Cell v9 = row.getCell(9);
                Cell v10 = row.getCell(10);
                Cell v11 = row.getCell(11);
                Cell v12 = row.getCell(12);
                Cell v13 = row.getCell(13);
                Cell v14 = row.getCell(14);
                Cell v15 = row.getCell(15);
                Cell v16 = row.getCell(16);
                Cell v17 = row.getCell(17);
                Cell v18 = row.getCell(18);
                Cell v19 = row.getCell(19);
                Cell v20 = row.getCell(20);
                Cell v21 = row.getCell(21);
                Cell v22 = row.getCell(22);
                Cell v23 = row.getCell(23);
                HashMap<String, String> map = new HashMap<String, String>();
                if (v0 != null)
                    map.put(getExcelTitle(sheet,formulaEvaluator,0),convertCellValueToString(v0));
                if (v1 != null)
                    map.put(getExcelTitle(sheet,formulaEvaluator,1),
                            convertCellValueToString(v1));
                if (v2 != null)
                    map.put(getExcelTitle(sheet,formulaEvaluator,2),
                            convertCellValueToString(v2));
                if (v3 != null)
                    map.put(getExcelTitle(sheet,formulaEvaluator,3),
                            convertCellValueToString(v3));
                if (v4 != null)
                    map.put(getExcelTitle(sheet,formulaEvaluator,4),
                            convertCellValueToString(v4));
                if (v5 != null)
                    map.put(getExcelTitle(sheet,formulaEvaluator,5),
                            convertCellValueToString(v5));
                if (v6 != null)
                    map.put(getExcelTitle(sheet,formulaEvaluator,6),
                            convertCellValueToString(v6));
                if (v7 != null)
                    map.put(getExcelTitle(sheet,formulaEvaluator,7),
                            convertCellValueToString(v7));
                if (v8 != null)
                    map.put(getExcelTitle(sheet,formulaEvaluator,8),
                            convertCellValueToString(v8));
                if (v9 != null)
                    map.put(getExcelTitle(sheet,formulaEvaluator,9),
                            convertCellValueToString(v9));
                if (v10 != null)
                    map.put(getExcelTitle(sheet,formulaEvaluator,10),
                            convertCellValueToString(v10));
                if (v11 != null)
                    map.put(getExcelTitle(sheet,formulaEvaluator,11),
                            convertCellValueToString(v11));
                if (v12 != null)
                    map.put(getExcelTitle(sheet,formulaEvaluator,12),
                            convertCellValueToString(v12));
                if (v13 != null)
                    map.put(getExcelTitle(sheet,formulaEvaluator,13),
                            convertCellValueToString(v13));
                if (v14 != null)
                    map.put(getExcelTitle(sheet,formulaEvaluator,14),
                            convertCellValueToString(v14));
                if (v15 != null)
                    map.put(getExcelTitle(sheet,formulaEvaluator,15),
                            convertCellValueToString(v15));
                if (v16 != null)
                    map.put(getExcelTitle(sheet,formulaEvaluator,16),
                            convertCellValueToString(v16));
                if (v17 != null)
                    map.put(getExcelTitle(sheet,formulaEvaluator,17),
                            convertCellValueToString(v17));
                if (v18 != null)
                    map.put(getExcelTitle(sheet,formulaEvaluator,18),
                            convertCellValueToString(v18));
                if (v19 != null)
                    map.put(getExcelTitle(sheet,formulaEvaluator,19),
                            convertCellValueToString(v19));
                if (v20 != null)
                    map.put(getExcelTitle(sheet,formulaEvaluator,20),
                            convertCellValueToString(v20));
                if (v21 != null)
                    map.put(getExcelTitle(sheet,formulaEvaluator,21),
                            convertCellValueToString(v21));
                if (v22 != null)
                    map.put(getExcelTitle(sheet,formulaEvaluator,22),
                            convertCellValueToString(v22));
                if (v23 != null)
                    map.put(getExcelTitle(sheet,formulaEvaluator,23),
                            convertCellValueToString(v23));
                res.add(map);
                // Log.i("Excel", "readExcel: " + v0.getStringValue() + "," +
                // v1.getStringValue());
            }
            // workbook.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.d("doc", e.getMessage());
        }
        // TODO Auto-generated method stub
        return res;
    }


    private String getExcelTitle(XSSFSheet sheet,FormulaEvaluator formulaEvaluator,int cellnum){
        CellValue xssfCell  = formulaEvaluator.evaluate(sheet.getRow(0).getCell(cellnum));
        if (null != xssfCell) {
            return xssfCell.getStringValue();
        }else{
            return "";
        }
    }

    private String convertCellValueToString(Cell cell) {
        if (cell == null) {
            return null;
        }
        String returnValue = null;
        switch (cell.getCellType()) {
            case 0: // 数字
                Double doubleValue = cell.getNumericCellValue();

                // 格式化科学计数法，取一位整数
                DecimalFormat df = new DecimalFormat("0");
                returnValue = df.format(doubleValue);
                break;
            case 1: // 字符串
                returnValue = cell.getStringCellValue();
                break;
            case 4: // 布尔
                Boolean booleanValue = cell.getBooleanCellValue();
                returnValue = booleanValue.toString();
                break;
            case 3: // 空值
                break;
            case 2: // 公式
                returnValue = cell.getCellFormula();
                break;
            case 5: // 故障
                break;
            default:
                break;
        }
        return returnValue;
    }


    public interface DetailHelperListener{
       void parserSuccess(ArrayList<HashMap<String, String>> dataList);
    }
}
